//
//  AuctionTreasureTableViewCell.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/1/4.
//  Copyright © 2020年 sun. All rights reserved.
//

#import "AuctionTreasureTableViewCell.h"

#pragma mark -- mark 数据相关
#import "AuctionSellModel.h"

@interface AuctionTreasureTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UIButton *enderShopBtn;
@property (nonatomic, strong) UICollectionView *collectionView;
@end
static NSString *const identifier = @"cell_c_cell_identifier";
@implementation AuctionTreasureTableViewCell
@synthesize iconImageView = _iconImageView;

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initContentViews];
    }
    return self;
}

- (void)initContentViews {
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.masksToBounds = true;
    contentView.layer.cornerRadius = 4.0;
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.mas_offset(10);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(contentView).mas_offset(12);
        make.size.mas_equalTo(CGSizeMake(39, 39));
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView);
        make.left.mas_equalTo(self.iconImageView.mas_right).mas_offset(12);
        make.right.mas_lessThanOrEqualTo(self.enderShopBtn.mas_left).mas_offset(-12);
    }];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel);
        make.bottom.mas_equalTo(self.iconImageView);
        make.right.mas_lessThanOrEqualTo(self.enderShopBtn.mas_left).mas_offset(-12);
    }];
    
    [self.enderShopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(contentView.mas_right).mas_offset(-12);
        make.centerY.mas_equalTo(self.iconImageView);
        make.size.mas_equalTo(CGSizeMake(50, 23));
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(contentView).mas_offset(12);
        make.right.mas_equalTo(contentView).mas_offset(-12);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).mas_offset(12);
        make.bottom.mas_equalTo(contentView).mas_offset(-12);
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.enderShopBtn.layer.cornerRadius = self.enderShopBtn.height * 0.5;
}
#pragma mark -- mark 懒加载
- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"auction_treasure_shop_icon_default")];
        _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:_iconImageView];
    }
    return _iconImageView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = SKColorWithHex(0x333333);
        _nameLabel.font = SKFONT_Medium(15);
        _nameLabel.text = @" ";
        [self addSubview:_nameLabel];
    }
    return _nameLabel;
}
- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = SKFONT_SIZE(13);
        _detailLabel.textColor = SKColorWithHex(0xB1B1B1);
        [self addSubview:_detailLabel];
    }
    return _detailLabel;
}
- (UIButton *)enderShopBtn {
    if (!_enderShopBtn) {
        _enderShopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _enderShopBtn.layer.borderColor = SKColorWithHex(0xFF465D).CGColor;
        _enderShopBtn.layer.borderWidth = 0.5;
        _enderShopBtn.layer.masksToBounds = true;
        [_enderShopBtn setTitle:@"进店" forState:UIControlStateNormal];
        _enderShopBtn.titleLabel.font = SKFONT_SIZE(13);
        [_enderShopBtn setTitleColor:SKColorWithHex(0xFF465D) forState:UIControlStateNormal];
        [_enderShopBtn addTarget:self action:@selector(enderShopButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_enderShopBtn];
    }
    return _enderShopBtn;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 6.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.showsVerticalScrollIndicator = false;
        _collectionView.showsHorizontalScrollIndicator = false;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[AuctionTreasureCollectionViewCell class] forCellWithReuseIdentifier:identifier];
        [self addSubview:_collectionView];
    }
    return _collectionView;
}


#pragma mark -- mark UICollectionViewDelegate UICollectionViewDataSource

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AuctionTreasureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    AuctionSellDataModel *model = self.dataArray[indexPath.item];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:SKIMAGE_NAMED(@"cell_default_img")];
    cell.price = model.price;
    cell.status = [model.status intValue];
    cell.sinceStartTimeInterval = model.lost_begin_time;
    cell.sinceEndTimeInterval = model.lost_end_time;
    cell.countDownEndBlock = self.countDownEndBlock;
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.collectionView.height, self.collectionView.height);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.cellDidSelectBlock) {
        self.cellDidSelectBlock(indexPath.item);
    }
}
#pragma mark -- mark EVENT

/**
 进入店铺

 @param sender sender
 */
- (void)enderShopButtonClick:(UIButton *)sender {
    if (self.enterShopButtonClickBlock) {
        self.enterShopButtonClickBlock();
    }
}


#pragma mark -- mark 业务逻辑
- (void)setTitle:(NSString *)title {
    _title = title;
    self.nameLabel.text = title;
}
- (void)setDetail:(NSString *)detail {
    _detail = detail;
    self.detailLabel.text = detail;
}
- (void)setDataArray:(NSArray<AuctionSellDataModel *> *)dataArray {
    _dataArray = dataArray;
    [self.collectionView reloadData];
}
@end
